﻿Imports Microsoft.Xna.Framework.Graphics
Imports Microsoft.Xna.Framework

Class TransitionScreen
    Inherits GameScreen

    Private mTransition As TransitionBase
    Private mOldScreen As GameScreen
    Private mNewScreen As GameScreen

    Private mCallBack As SimpleCallback

    Public Sub New(ByVal GameManager As GameManager, ByVal OldScreen As GameScreen, ByVal NewScreen As GameScreen, ByVal OnComplete As SimpleCallback, ByVal TransitionTime As Single)
        MyBase.New(GameManager)
        mTransition = New TransitionBase(TransitionTime, OnComplete)
        mOldScreen = OldScreen
        mNewScreen = NewScreen
        mCallBack = OnComplete
    End Sub

    Public Sub New(ByVal GameManager As GameManager, ByVal OldScreen As GameScreen, ByVal NewScreen As GameScreen, ByVal OnComplete As SimpleCallback)
        MyClass.New(GameManager, OldScreen, NewScreen, OnComplete, 1.0F)
    End Sub

#Region " Properties "
    Public Overrides ReadOnly Property ScreenName() As String
        Get
            ScreenName = "TransitionScreen"
        End Get
    End Property

    Public ReadOnly Property OldScreen() As GameScreen
        Get
            OldScreen = mOldScreen
        End Get
    End Property

    Public ReadOnly Property NewScreen() As GameScreen
        Get
            NewScreen = mNewScreen
        End Get
    End Property

    Public ReadOnly Property Transition() As TransitionBase
        Get
            Transition = mTransition
        End Get
    End Property

#End Region

    Public Overrides Sub Update(ByVal elapsedSeconds As Single)
        MyBase.Update(elapsedSeconds)
        mTransition.Update(elapsedSeconds)
    End Sub

    Public Overrides Sub Draw(ByVal elapsedSeconds As Single)
        MyBase.Draw(elapsedSeconds)

        If Transition.FractionComplete <= 0.5 Then
            ' Fade to black
            OldScreen.Draw(elapsedSeconds)
        Else
            ' Fade in
            NewScreen.Draw(elapsedSeconds)
        End If
        FadeBackBufferToBlack(255 - Transition.TransitionAlphaByHalves)
    End Sub

    ''' <summary>
    ''' Helper draws a translucent black fullscreen sprite, used for fading
    ''' screens in and out, and for darkening the background behind popups.
    ''' </summary>
    Private Sub FadeBackBufferToBlack(ByVal alpha As Integer)

        Dim Viewport As Viewport = GameManager.GraphicsDevice.Viewport

        GameManager.SpriteBatch.Draw(GameManager.Sprites("blank"), _
                         New Rectangle(0, 0, Viewport.Width, Viewport.Height), _
                         New Color(0, 0, 0, CByte(alpha)))

    End Sub

    Private Sub TransitionCompleteCallback()
        mCallBack.Invoke()
    End Sub

End Class